package hku.cs.lunuo.rank;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class EsStatistic extends Statistic {
	private HashMap<String, HashMap<String, HashMap<String, StatisticProperty>>> data = 
		new HashMap<String, HashMap<String, HashMap<String, StatisticProperty>>>();

	public void add(String className, String methodName, String es,
			boolean tof) {
		HashMap<String, HashMap<String, StatisticProperty>> HES;
		HES = data.get(className);
		if(HES == null) {
			HES = new HashMap<String, HashMap<String, StatisticProperty>>();
		}
		HashMap<String, StatisticProperty> inHES;
		inHES = HES.get(methodName);
		if(inHES==null) {
			inHES = new HashMap<String, StatisticProperty>();
		}
		StatisticProperty cbs = inHES.get(es);
		if(cbs == null) {
			cbs = new StatisticProperty();
		}
		
		cbs.add(tof);	
		inHES.put(es, cbs);
		HES.put(methodName, inHES);
		data.put(className, HES);
	}
	
	
	public Set<String> getKlassNames() {
		return data.keySet();
	}
	public Set<String> getMethodNames(String klassName) {
		return data.get(klassName).keySet();
	}
	public Set<String> getEss(String klassName, String methodName) {
		return data.get(klassName).get(methodName).keySet();
	}
	public StatisticProperty getProperty(String klassName, String methodName, String es) {
		return data.get(klassName).get(methodName).get(es);
	}
	public void setLabel(String klassName, String methodName, String es, Label label) {
		data.get(klassName).get(methodName).get(es).label = label;
		
	}
	
	public void toXML(File f) {	
		Element xmlRoot = new Element("EsStatistics");
		Document xmlDoc = new Document(xmlRoot);
		int i=0;
		for(String klassName : data.keySet()) {
			for(String methodName: data.get(klassName).keySet()) {
				for(String es : data.get(klassName).get(methodName).keySet()) {
					Element xes = new Element("es");
					xes.setAttribute("class", klassName);
					xes.setAttribute("method", methodName);
					xes.setAttribute("es", es);
					data.get(klassName).get(methodName).get(es).toXML(xes);
					xmlRoot.addContent(xes);
					i++;
					xes.setAttribute("index", Integer.toString(i));
				}
			}
		}
		xmlRoot.setAttribute("number", String.valueOf(i));
		
		try {
			new XMLOutputter().output(xmlDoc, new FileOutputStream(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void toSortedXML(File f) {
		List<EsStatisticUnit> tmpData = new ArrayList<EsStatisticUnit>();
		for(String klassName : data.keySet()) {
			for(String methodName: data.get(klassName).keySet()) {
				for(String es : data.get(klassName).get(methodName).keySet()) {
					tmpData.add(new EsStatisticUnit(klassName, methodName, es, data.get(klassName).get(methodName).get(es)));
				}
			}
		}
		Collections.sort(tmpData);
		Collections.reverse(tmpData);
		
		Element xmlRoot = new Element("EsStatistics");
		Document xmlDoc = new Document(xmlRoot);
		xmlRoot.setAttribute("number", String.valueOf(tmpData.size()));
		int i = 0;
		for(EsStatisticUnit esu : tmpData) {
			Element xml = new Element("es");
			xml.setAttribute("index", Integer.toString(++i));
			esu.toXML(xml);
			xmlRoot.addContent(xml);
		}
		
		try {
			new XMLOutputter().output(xmlDoc, new FileOutputStream(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
}
